package com.kamikaze;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


public class OvertimeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
  
    public OvertimeServlet() {
        super();

    }


	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request,response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("utf-8");
		request.setCharacterEncoding("utf-8");
		
		
		String user_id = request.getParameter("user_id"); 
		String otd_date = request.getParameter("otd_date"); 
		String otd_time_begin = request.getParameter("otd_time_begin"); 
		String otd_time_end = request.getParameter("otd_time_end"); 
		String ot_number = request.getParameter("ot_number"); 
		String checker_id = request.getParameter("checker_id"); 
		String otd_note = request.getParameter("otd_note"); 
		Overtime overtime = new Overtime();
		HttpSession session = request.getSession();
//取shift_number
		int shift_number = overtime.getShift_number(user_id,otd_date);
		if(shift_number == 0){ 
			session.setAttribute("overtime", overtime);
			response.sendRedirect("overtime.jsp"); 
		}
//檢查加班日期有沒有重複
		boolean pass = overtime.check(shift_number,otd_date);
		if(pass == true){
//將加班資料寫入資料庫		
			boolean succ = overtime.process
					(user_id, otd_date, otd_time_begin, otd_time_end, ot_number, checker_id, otd_note, shift_number);
			if(succ){
				session.setAttribute("overtime", overtime);
				response.sendRedirect("overtime.jsp");
			}
		}
		else{
			session.setAttribute("overtime", overtime);
			response.sendRedirect("overtime.jsp");
		}
	}

}
